using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BHXH.Core.Domain;
using BHXH.Core.IService;
using FX.Data;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;
namespace BHXH.Core.ServiceImp
{
    public class taikhoanService : BaseService<taikhoan, int>, ItaikhoanService
    {
        static ISession mySession;
        public taikhoanService(string sessionFactoryConfigPath)
            : base(sessionFactoryConfigPath)
        { }

        public bool checkLogin(string TaiKhoan, string MatKhau)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                IList<taikhoan> query = mySession.CreateQuery("FROM taikhoan as t where t.TaiKhoan = :tk and t.MatKhau = :mk")
                    .SetParameter("tk", TaiKhoan)
                    .SetParameter("mk", MatKhau)
                    .List<taikhoan>();
                if (query.Count > 0)
                    return true;
                else
                    return false;
                
            }
        }

        public bool UpdateTaiKhoan(taikhoan tk)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                var a = Update(tk);
                CommitChanges();
                mySession.Clear();
                return true;
            }

        }

        public int Update_MatKhauTaiKhoan(taikhoan tk)
        {

            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                int a = mySession.CreateQuery("Update taikhoan set MatKhau =:matkhau where ID =:id")
                    .SetParameter("matkhau", tk.MatKhau)
                    .SetParameter("id", tk.ID)
                    .ExecuteUpdate();
                CommitChanges();
                mySession.Clear();
                return a;
            }
        }
    }

}